﻿Public Class PromitheutesControl


    Private Function keepetaira() As String
        Dim fileReader As String = "none"
        Try
            fileReader = My.Computer.FileSystem.ReadAllText(Application.StartupPath & _
                                            "\keppetairia.txt")
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return fileReader
    End Function
    Private Sub PromitheytesBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PromitheytesBindingNavigatorSaveItem.Click
        If SaveChanges() Then
            MessageBox.Show("Your changes were saved", _
              "Promitheutes", MessageBoxButtons.OK)
        End If

    End Sub

    Private Function SaveChanges() As Boolean
        If ValidateData() Then
            If OnomaTextBox.Text <> String.Empty Then
                If Convert.ToInt32( _
                  EkptosiTextBox.Text) > 100 Then

                    MessageBox.Show( _
                      "H ekptwsh d mporei na einai " & _
                      "megaluterh apo 100", _
                      "Promitheutes", MessageBoxButtons.OK, _
                      MessageBoxIcon.Information)
                    Return False
                End If
            End If

            Me.TableAdapterManager.UpdateAll( _
              Me.Hard_oneDataSet)
            Return True
        End If
        Return False
    End Function

    Private Function ValidateData() As Boolean
        If Me.Validate() Then
            Try
                PromitheytesBindingSource.EndEdit()
                Return True
            Catch ex As Exception
                If MessageBox.Show( _
                  "Invalid data was entered. Cancel edits?", _
                  "Promitheutes", MessageBoxButtons.YesNo, _
                  MessageBoxIcon.Error) = DialogResult.Yes Then

                    PromitheytesBindingSource.CancelEdit()
                End If
                Return False
            End Try
        End If
        Return False
    End Function

    Protected Overloads Overrides Sub OnCreateControl()
        MyBase.OnCreateControl()
        AddHandler Me.ParentForm.FormClosing, AddressOf ParentForm_FormClosing
    End Sub

    Private Sub ParentForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
        If ValidateData() Then
            If Me.Hard_oneDataSet.promitheytes.GetChanges _
                IsNot Nothing Then

                Dim response As Integer = _
                  MessageBox.Show( _
                  "Do you want to save your changes?", _
                  "Changes have been made", _
                  MessageBoxButtons.YesNoCancel, _
                  MessageBoxIcon.Warning)

                If response = DialogResult.Yes Then
                    If SaveChanges() Then
                        MessageBox.Show("Your changes were saved", _
                          "Promitheutes", MessageBoxButtons.OK)
                    Else
                        e.Cancel = True
                    End If
                ElseIf response = DialogResult.Cancel Then
                    e.Cancel = True
                End If
            End If
        End If
    End Sub

    Private Sub PromitheutesControl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.PromitheytesTableAdapter.FillByName(Me.Hard_oneDataSet.promitheytes, "%" & TextBox1.Text & "%", "%" & keepetaira() & "%")
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Me.PromitheytesTableAdapter.FillByName(Me.Hard_oneDataSet.promitheytes, "%" & TextBox1.Text & "%", "%" & keepetaira() & "%")
    End Sub

    Private Sub TextBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Click
        Me.PromitheytesTableAdapter.FillByName(Me.Hard_oneDataSet.promitheytes, "%" & TextBox1.Text & "%", "%" & keepetaira() & "%")
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Me.PromitheytesTableAdapter.Fill(Me.Hard_oneDataSet.promitheytes)
    End Sub
End Class
